﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class LocationTemplate : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlType01;
        protected DropDownList ddlType02;
        protected DropDownList ddlType03;
        protected DropDownList ddlType04;
        protected DropDownList ddlType05;
        protected HtmlForm F2141;
        protected HtmlInputHidden hdnFrom;
        protected Label lblDesc;
        protected Label lblField01;
        protected Label lblField02;
        protected Label lblField03;
        protected Label lblField04;
        protected Label lblField05;
        protected Label lblLength;
        protected Label lblLocTemplate;
        protected Label lblLocTemplateID;
        protected Label lblMsg;
        protected Label lblSeperatorChar;
        protected Label lblType;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        private List<SysCode4DDL> sysCode4DDL = null;
        protected TextBox txtDesc01;
        protected TextBox txtDesc02;
        protected TextBox txtDesc03;
        protected TextBox txtDesc04;
        protected TextBox txtDesc05;
        protected TextBox txtLength01;
        protected TextBox txtLength02;
        protected TextBox txtLength03;
        protected TextBox txtLength04;
        protected TextBox txtLength05;
        protected TextBox txtLocTemplateID;
        protected TextBox txtSeperatorChar;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtLocTemplateID.Text = "";
            this.txtLocTemplateID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtLocTemplateID.Text.Trim()))
            {
                try
                {
                    List<string> locTemplateID = new List<string> {
                        this.txtLocTemplateID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(locTemplateID, "LOCATION_TEMPLATE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("LocationTemplateQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("LocationTemplateList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLocTemplateID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2320_005");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateLocTemplateInfo();
            }
            else
            {
                this.InsertLocTemplateInfo();
            }
        }

        private LocationTemplateInfo GetLocTemplateFromForm(LocationTemplateInfo locTemplate)
        {
            locTemplate.LocTemplateID = this.txtLocTemplateID.Text.Trim();
            locTemplate.SeperatorChar = this.txtSeperatorChar.Text.Trim();
            locTemplate.Field1Length = int.Parse((this.txtLength01.Text.Trim() == "") ? "0" : this.txtLength01.Text.Trim());
            locTemplate.Field1Type = this.ddlType01.SelectedValue;
            locTemplate.Field1Desc = this.txtDesc01.Text.Trim();
            locTemplate.Field2Length = int.Parse((this.txtLength02.Text.Trim() == "") ? "0" : this.txtLength02.Text.Trim());
            locTemplate.Field2Type = this.ddlType02.SelectedValue;
            locTemplate.Field2Desc = this.txtDesc02.Text.Trim();
            locTemplate.Field3Length = int.Parse((this.txtLength03.Text.Trim() == "") ? "0" : this.txtLength03.Text.Trim());
            locTemplate.Field3Type = this.ddlType03.SelectedValue;
            locTemplate.Field3Desc = this.txtDesc03.Text.Trim();
            locTemplate.Field4Length = int.Parse((this.txtLength04.Text.Trim() == "") ? "0" : this.txtLength04.Text.Trim());
            locTemplate.Field4Type = this.ddlType04.SelectedValue;
            locTemplate.Field4Desc = this.txtDesc04.Text.Trim();
            locTemplate.Field5Length = int.Parse((this.txtLength05.Text.Trim() == "") ? "0" : this.txtLength05.Text.Trim());
            locTemplate.Field5Type = this.ddlType05.SelectedValue;
            locTemplate.Field5Desc = this.txtDesc05.Text.Trim();
            locTemplate.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            return locTemplate;
        }

        private void InitTypeDropDownLit(DropDownList ddl)
        {
            if (this.sysCode4DDL == null)
            {
                this.sysCode4DDL = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("LocFieldType");
            }
            ddl.DataSource = this.sysCode4DDL;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void InsertLocTemplateInfo()
        {
            try
            {
                LocationTemplateInfo locTemplateInfo = new LocationTemplateInfo();
                locTemplateInfo = this.GetLocTemplateFromForm(locTemplateInfo);
                locTemplateInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(locTemplateInfo, "LOCATION_TEMPLATE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = locTemplateInfo;
                this.txtLocTemplateID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            this.InitTypeDropDownLit(this.ddlType01);
            this.InitTypeDropDownLit(this.ddlType02);
            this.InitTypeDropDownLit(this.ddlType03);
            this.InitTypeDropDownLit(this.ddlType04);
            this.InitTypeDropDownLit(this.ddlType05);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetLocTemplateInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLocTemplateInfo2Form()
        {
            try
            {
                string locTemplateID = base.Request.QueryString["ID"].ToString();
                LocationTemplateInfo locTemplate = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(locTemplateID, "LOCATION_TEMPLATE") as LocationTemplateInfo;
                base.DataEntityViewState = locTemplate;
                if (locTemplate != null)
                {
                    this.SetLocTemplateInfo2Form(locTemplate);
                    this.txtLocTemplateID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetLocTemplateInfo2Form(LocationTemplateInfo locTemplate)
        {
            this.txtLocTemplateID.Text = locTemplate.LocTemplateID;
            this.txtSeperatorChar.Text = locTemplate.SeperatorChar;
            this.txtLength01.Text = (locTemplate.Field1Length.ToString() == "0") ? "" : locTemplate.Field1Length.ToString();
            this.ddlType01.SelectedValue = locTemplate.Field1Type;
            this.txtDesc01.Text = locTemplate.Field1Desc;
            this.txtLength02.Text = (locTemplate.Field2Length.ToString() == "0") ? "" : locTemplate.Field2Length.ToString();
            this.ddlType02.SelectedValue = locTemplate.Field2Type;
            this.txtDesc02.Text = locTemplate.Field2Desc;
            this.txtLength03.Text = (locTemplate.Field3Length.ToString() == "0") ? "" : locTemplate.Field3Length.ToString();
            this.ddlType03.SelectedValue = locTemplate.Field3Type;
            this.txtDesc03.Text = locTemplate.Field3Desc;
            this.txtLength04.Text = (locTemplate.Field4Length.ToString() == "0") ? "" : locTemplate.Field4Length.ToString();
            this.ddlType04.SelectedValue = locTemplate.Field4Type;
            this.txtDesc04.Text = locTemplate.Field4Desc;
            this.txtLength05.Text = (locTemplate.Field5Length.ToString() == "0") ? "" : locTemplate.Field5Length.ToString();
            this.ddlType05.SelectedValue = locTemplate.Field5Type;
            this.txtDesc05.Text = locTemplate.Field5Desc;
            this.chkIsActive.Checked = locTemplate.IsActive == "Y";
        }

        private void UpdateLocTemplateInfo()
        {
            try
            {
                LocationTemplateInfo locTemplateInfo = base.DataEntityViewState as LocationTemplateInfo;
                locTemplateInfo = this.GetLocTemplateFromForm(locTemplateInfo);
                locTemplateInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(locTemplateInfo, "LOCATION_TEMPLATE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

